import FSM from "./FSM";
import FSMTransition from "./FSMTransition";

export class FSMState {
    public name: string;
    public id: number;
    public fsm: FSM;
    private _transitions: FSMTransition[];

    public init() {
        this._transitions = [];
    }
    
    public onEnter() {

    }

    public update(dt) {

    }

    public onEixt() {

    }

    public tick() {
        for (let i = 0, len = this._transitions.length; i < len; i++) {
            if (this._transitions[i].tick(this.fsm.data)) {
                return this._transitions[i].toId;
            }
        }
        return -1;
    }

    public addTransition(toId: number) {
        let transition = this.getTransition(toId);
        if (!transition) {
            transition = new FSMTransition(this.id, toId);
            this._transitions.push(transition);
            return transition;
        }
        return null;
    }

    public getTransition(toId: number) {
        let result: FSMTransition;
        for (let i = 0, len = this._transitions.length; i < len; i++) {
            if (this._transitions[i].toId === toId) {
                result = this._transitions[i];
                break;
            }
        }
        return result;
    }
}